package com.jwell.panrank.presenter

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jwell.driverapp.tools.BaseSubscriber
import com.jwell.driverapp.ui.base.BasePresenter
import com.jwell.panrank.contract.CarInquireContract
import com.jwell.panrank.contract.CarNumberContract
import com.jwell.panrank.entity.CarInquireBean
import com.jwell.panrank.entity.CarNumberBean
import com.jwell.panrank.model.CarInquireModel
import org.json.JSONObject
import org.reactivestreams.Subscription

/**
 * Created by 76577 on 2018/6/19.
 * 马坎
 */
class CarNumberPresenter : CarNumberContract.Presenter, BasePresenter<CarNumberContract.View>() {
    private val mode: CarInquireModel by lazy {
        CarInquireModel()
    }

    override fun searchListData() {
        mode.getMaKanCount(object : BaseSubscriber(getView()) {
            override fun setSubscription(s: Subscription) {
                mSubscription = s
            }

            override fun succeed(data: JSONObject) {
                getView()?.hideProgress()
                val beanList = Gson().fromJson<List<CarNumberBean>>(data.getString("result"), object : TypeToken<List<CarNumberBean>>() {
                }.type)
                if (index == 0) {
                    getView()?.showListData(beanList, true)
                } else {
                    getView()?.showListData(beanList, false)
                }
                index++

            }

            override fun onFailed(msg: String) {
                getView()?.hideProgress()
                if (index == 0) {
                    getView()?.showListDataFailed(true, msg)
                } else {
                    getView()?.showListDataFailed(false, msg)
                }
            }
        })
    }
}